namespace Security.Business
{
    using System;
    using Security.Entities;
    using Security.Data;
    using System.Collections.Generic;
    using DataAccess;
    using Common.Errors;

    public class UserService
    {
        public static IList<RoleDescriptor> GetRoles(Guid userId, bool includeInGroups)
        {
            if (includeInGroups)
            {
                GetAllRolesSP sp = new GetAllRolesSP();
                sp.UserId = userId;
                return sp.Execute();
            }
            else
            {
                GetRolesSP sp = new GetRolesSP();
                sp.UserId = userId;
                return sp.Execute();
            }
        }

        public static IList<GroupDescriptor> GetGroups(Guid userId)
        {
            GetGroupsSP sp = new GetGroupsSP();
            sp.UserId = userId;
            return sp.Execute();
        }

        public static bool AddRole(Guid userId, string roleName)
        {
            RoleDAO dao = new RoleDAO();
            Role role = dao.GetByName(roleName);
            if (role != null)
            {
                return AddRole(userId, role.RoleId);
            }
            else
            {
                throw new InputError("No se encuentra la patente " + roleName);
            }
        }

        public static bool AddRole(Guid userId, Guid roleId)
        {
            AddUserToRoleSP sp = new AddUserToRoleSP();
            sp.UserId = userId;
            sp.RoleId = roleId;
            int cnt = sp.Execute();
            if (cnt == 1)
            {
                return true;
            }
            else
            {
                throw new DBError("No se pudo agregar la patente");
            }
        }

        public static bool RemoveRole(Guid userId, string roleName)
        {
            RoleDAO dao = new RoleDAO();
            Role role = dao.GetByName(roleName);
            if (role != null)
            {
                return RemoveRole(userId, role.RoleId);
            }
            else
            {
                throw new InputError("No se encuentra la patente " + roleName);
            }
        }

        public static bool RemoveRole(Guid userId, Guid roleId)
        {
            RemoveUserFromRoleSP sp = new RemoveUserFromRoleSP();
            sp.UserId = userId;
            sp.RoleId = roleId;
            int cnt = sp.Execute();
            if (cnt == 1)
            {
                return true;
            }
            else
            {
                throw new DBError("No se pudo quitar la patente");
            }
        }

        public static bool RemoveGroup(Guid userId, string groupName)
        {
            GroupDAO dao = new GroupDAO();
            Group group = dao.GetByName(groupName);
            if (group != null)
            {
                return RemoveRole(userId, group.GroupId);
            }
            else
            {
                throw new InputError("No se encuentra la familia " + groupName);
            }
        }

        public static bool RemoveGroup(Guid userId, Guid groupId)
        {
            RemoveUserFromGroupSP sp = new RemoveUserFromGroupSP();
            sp.UserId = userId;
            sp.GroupId = groupId;
            int cnt = sp.Execute();
            if (cnt == 1)
            {
                return true;
            }
            else
            {
                throw new DBError("No se pudo quitar la familia");
            }
        }

        public static bool AddGroup(Guid userId, string groupName)
        {
            GroupDAO dao = new GroupDAO();
            Group group = dao.GetByName(groupName);
            if (group != null)
            {
                return AddGroup(userId, group.GroupId);
            }
            else
            {
                throw new InputError("No se encuentra la familia " + groupName);
            }
        }

        public static bool AddGroup(Guid userId, Guid groupId)
        {
            AddUserToGroupSP sp = new AddUserToGroupSP();
            sp.UserId = userId;
            sp.GroupId = groupId;
            int cnt = sp.Execute();
            if (cnt == 1)
            {
                return true;
            }
            else
            {
                throw new DBError("No se pudo agregar la familia");
            }
        }
    }
}
